/** @file window.cpp
 */
#include "window.h"

/** @brief  create a dialog, with parent =hWnd
 */
Window::Window(HWND parent):Widget(parent)
{
    activable=1;

}

Window::~Window()
{
}

void Window::draw(Rect *r)
{
    pDC->fillRect(rect.x0,rect.y0, rect.x1,rect.y1, 0x0);

    pDC->dirty(max(r->x0,rect.x0), max(r->y0,rect.y0), min(r->x1,rect.x1), min(r->y1,rect.y1));
    Widget::draw(r);
}


int Window::run()
{
    Msg msg;
    
    create();
    while( 1)
    {
        if(lifeState ==0)
        {
            return lifeState;
        }
        if(lifeState == -1)
        {
            return lifeState;
        }
        
        if(::getMessage(&msg))
        {
            if( (msg.iMsg ==MSG_KEY_UP) || (msg.iMsg ==MSG_KEY_DOWN) ) msg.hWnd =this;
            sendMessage(msg.hWnd, msg.iMsg, msg.wParam, msg.lParam);
        }
    }
}

/** @brief dialog event handler
 */
int Window::eventHandler(HWND hWnd, int iMsg, WPARAM wParam, LPARAM lParam)
{

    switch(iMsg)
    {
    case MSG_KEY_DOWN:
        //Child have the Active status
        if(activeChild)
        {
            sendMessage( activeChild, iMsg, wParam, lParam);
        }
        
        //need key focus
        else
        {
            //need key focus, then change key focus status by KEY[Enter]
            switch(wParam)
            {
            case KEY_ENTER:
                activeFirstChild();
                break;
            case KEY_PREV:
                sendMessage(parent, MSG_ACTIVE_ELDER_CHILD, 0,0);
                break;
            case KEY_NEXT:
                sendMessage(parent, MSG_ACTIVE_YOUNGER_CHILD, 0,0);
                break;
            }
            
        }
        break;
        
    case MSG_KEY_UP:
        if(activeChild)
        {
            sendMessage( activeChild, iMsg, wParam, lParam);
        }
        break;

        //father's duty
    case MSG_ACTIVE_ELDER_CHILD:
        activePrevChild();
        break;
        
    case MSG_ACTIVE_YOUNGER_CHILD:
        activeNextChild();
        break;
    }

    return 0;
}

